package com.example.casper.Experiment2024.data;
import android.content.Context;
import android.util.Log;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

public class DataBank {
    private final Context context;
    private final String data_file_name = "items.data";

    public DataBank(Context context) {
        this.context = context;
    }

    public List<book> readItems() {
//        context.openFileInput();
        List<book> items = new ArrayList<>();
        try {
            // 创建一个文件输入流
            FileInputStream fileIn = context.openFileInput(data_file_name);

            // 创建一个对象输入流
            ObjectInputStream in = new ObjectInputStream(fileIn);

            // 从输入流中读取对象
            items = (ArrayList<book>) in.readObject();

            // 关闭输入流
            in.close();

            // 关闭文件输入流
            fileIn.close();


        } catch (IOException | ClassNotFoundException e) {

            Log.e("Experiment2024.data", e.toString());
        }
        return items;
    }

    public void saveItems(List<book> items) {
        FileOutputStream fileOut = null;
        try {
            fileOut = context.openFileOutput(data_file_name, Context.MODE_PRIVATE);
            ObjectOutputStream out = new ObjectOutputStream(fileOut);

            // 将对象写入输出流
            out.writeObject(items);

            // 关闭输出流
            out.close();

            // 关闭文件输出流
            fileOut.close();

            System.out.println("Person对象已序列化到person.ser文件");
        } catch (IOException e) {
            Log.e("Experiment2024.data", e.toString());
        }
    }
}
